import Vue from 'vue'
import Vuex from 'vuex'
// import { getGoodsList } from '../api/shop'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    num: 100,
    msg: 'aaa',
    shopInfo: {},
    goodsList: []
  },
  // 转变 改变
  mutations: {
    setVal(state, o) {
      state.shopInfo = o
    },
    setGoodsList(state, o) {
      state.goodsList = o
    },
    // 清空商品列表
    clearGoodsList(state) {
      for (const v of state.goodsList) {
        for (const k of v.foods) {
          if (k.num !== 0) {
            k.num = 0
          }
        }
      }
    }
  },
  actions: {},
  modules: {},
  getters: {
    // 这里写法跟过滤器一样，需要return一个结构
    // getter的作用是对原有数据进行过滤和包装，但是不操作数据
    getChooseGoods(state) {
      const arr = []

      for (const v of state.goodsList) {
        for (const k of v.foods) {
          if (k.num !== 0) {
            arr.push(k)
          }
        }
      }
      return arr
    }
  }
})
